package com.ruoyi.project.jiaqin.companyvideo.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jiaqin.companyvideo.domain.CompanyVideo;
import com.ruoyi.project.jiaqin.companyvideo.service.ICompanyVideoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 宣传视频Controller
 *
 * @author admin
 * @date 2021-01-27
 */
@Controller
@RequestMapping("/jiaqin/companyvideo")
public class CompanyVideoController extends BaseController
{
    private String prefix = "jiaqin/companyvideo";

    @Autowired
    private ICompanyVideoService companyVideoService;
    @Autowired
    private ICompanyService iCompanyService;

    @RequiresPermissions("jiaqin:companyvideo:view")
    @GetMapping()
    public String companyvideo()
    {
        return prefix + "/companyvideo";
    }

    /**
     * 查询宣传视频列表
     */
    @RequiresPermissions("jiaqin:companyvideo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CompanyVideo companyVideo)
    { Company company = iCompanyService.selectCompanyBySysUserId(getSysUser().getUserId());
        if(company != null) {
            companyVideo.setCompanyId(company.getId());
            startPage();
            List<CompanyVideo> list = companyVideoService.selectCompanyVideoList(companyVideo);
            return getDataTable(list);
        }
        if(getSysUser().isAdmin()){
            startPage();
            List<CompanyVideo> list = companyVideoService.selectCompanyVideoList(companyVideo);
            return getDataTable(list);
        }
        return new TableDataInfo();
    }

    /**
     * 导出宣传视频列表
     */
    @RequiresPermissions("jiaqin:companyvideo:export")
    @Log(title = "宣传视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CompanyVideo companyVideo)
    {
        List<CompanyVideo> list = companyVideoService.selectCompanyVideoList(companyVideo);
        ExcelUtil<CompanyVideo> util = new ExcelUtil<CompanyVideo>(CompanyVideo.class);
        return util.exportExcel(list, "companyvideo");
    }

    /**
     * 新增宣传视频
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        Company company = iCompanyService.selectCompanyBySysUserId(getSysUser().getUserId());
        if(company!=null){
            mmap.put("company",company);
            return prefix + "/addme";
        }
        return prefix + "/add";
    }

    /**
     * 新增保存宣传视频
     */
    @RequiresPermissions("jiaqin:companyvideo:add")
    @Log(title = "宣传视频", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CompanyVideo companyVideo)
    {
        //查公司是否存在
        Company company = iCompanyService.selectCompanyBySysUserId(getSysUser().getUserId());
        if(company!=null){
            //存在的话，查企业的简介条数条数超过一不能添加
            if((companyVideoService.selectCompanyVideoCountByCompanyId(company.getId()))>=1){
                return AjaxResult.error("企业宣传视频不能超一个");
            }
        }
        companyVideo.setCreateAt(new Date());
        return toAjax(companyVideoService.insertCompanyVideo(companyVideo));
    }

    /**
     * 修改宣传视频
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CompanyVideo companyVideo = companyVideoService.selectCompanyVideoById(id);
        mmap.put("companyVideo", companyVideo);
        return prefix + "/edit";
    }

    /**
     * 修改保存宣传视频
     */
    @RequiresPermissions("jiaqin:companyvideo:edit")
    @Log(title = "宣传视频", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CompanyVideo companyVideo)
    {
        return toAjax(companyVideoService.updateCompanyVideo(companyVideo));
    }

    /**
     * 删除宣传视频
     */
    @RequiresPermissions("jiaqin:companyvideo:remove")
    @Log(title = "宣传视频", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(companyVideoService.deleteCompanyVideoByIds(ids));
    }
}
